package 课设;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.ResultSetMetaData;
import java.sql.SQLException;
import java.util.ArrayList;


public class blogtable extends JFrame{
    private JTable table;
    private MyTableModel tablemodel;
    private JButton b1,b3,b4;
    private JToolBar tool;

    public blogtable() {
        this.setSize(800, 600);
        this.setTitle("blogMessage");
        this.setLocationRelativeTo(getOwner());


        tablemodel = getModel();
        table = new JTable(tablemodel);
        table.setPreferredScrollableViewportSize(new Dimension(500, 300));
        JScrollPane scroll = new JScrollPane(table);
        getContentPane().add(scroll, BorderLayout.CENTER);

        b1 = new JButton("添加");
        b1.setToolTipText("添加");
        b1.setFocusable(false);
        b1.setHorizontalTextPosition(SwingConstants.CENTER);
        b1.setVerticalTextPosition(SwingConstants.BOTTOM);


        b3 = new JButton("删除");
        b3.setToolTipText("删除");
        b3.setFocusable(false);
        b3.setHorizontalTextPosition(SwingConstants.CENTER);
        b3.setVerticalTextPosition(SwingConstants.BOTTOM);

        //创建工具栏，添加按钮
        tool = new JToolBar();
        tool.add(b1);
        tool.add(b3);
        tool.setRollover(true);

        //添加工具栏
        getContentPane().add(tool,BorderLayout.NORTH);
        b1.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                blogAdd padd = new blogAdd("");
                padd.setVisible(true);
                dispose();
            }
        });

        b3.addActionListener(new ActionListener() {//删
            public void actionPerformed(ActionEvent e) {
                db dbcon = new db();
                try {
                    if(table.getSelectedRows().length > 0) {
                        //获得JTable中选中行的序列
                        int[] selRowIndexs = table.getSelectedRows();
                        PreparedStatement presta = dbcon.PreparedStatement("delete from blog where blogId = ?");

                        for(int i = 0;i<selRowIndexs.length;i++) {
                            presta.setInt(1, (int)table.getValueAt(selRowIndexs[i], 0)); //presta.setString(1, table.getValueAt(______,0).toString());
                            presta.executeUpdate();
                        }
                        //重新加载数据到JTable
                        tablemodel = getModel();
                        table.setModel(tablemodel);
                    }
                }catch(SQLException sqle) {
                    System.out.println(sqle.toString());
                }
            }
        });

        b4 = new JButton("查看");
        b4.setToolTipText("查询");
        b4.setFocusable(false);
        b4.setHorizontalTextPosition(SwingConstants.CENTER);
        b4.setVerticalTextPosition(SwingConstants.BOTTOM);

        tool.add(b4);

        b4.addActionListener(new ActionListener() {//查看选中文件
            public void actionPerformed(ActionEvent e) {
                db dbcon;
                try{
                    dbcon = new db();
                    if(table.getSelectedRows().length > 0) {
                        //获得JTable中选中行的序列
                        int[] selRowIndexs = table.getSelectedRows();
                        if(selRowIndexs != null) {
                            int blogId = (int) table.getValueAt(selRowIndexs[0], 0);
                            System.out.println("-------");
                            manager_select a = new manager_select(blogId);
                            a.setVisible(true);
                        }
                    }else
                        JOptionPane.showMessageDialog(null,"未选中");
                }catch (Exception ex){
                    System.out.println(ex.getMessage());
                }finally {
                    tablemodel = getModel();
                    table.setModel(tablemodel);
                }
            }
        });
    }

    private MyTableModel getModel() {
        MyTableModel tableModel = new MyTableModel();
        db dbcon;
        try {
            dbcon = new db();
            ResultSet rs = dbcon.executeQuery("select * from blog");

            ResultSetMetaData rsmd = rs.getMetaData();//获得查询结果中的列名，填充表格模型列
            int Colnum = rsmd.getColumnCount();
            int i;
            for (i = 1; i <= Colnum; i++)
                tableModel.addColumn(rsmd.getColumnName(i));
            ArrayList<Blog> v = new ArrayList<>();//获取查询结果中的元组，填充表格模型行
            while (rs.next()) {
                Blog blog = new Blog();
                blog.setBlogId(rs.getInt("blogId"));
                blog.setTitle(rs.getString("title"));
                blog.setContent(rs.getString("summary"));
                blog.setPostTime(rs.getTimestamp("postTime"));
                blog.setUserId(rs.getInt("userId"));
                v.add(blog);
            }
            rs.close();
            for (i = 0; i < v.size(); i++) {
                tableModel.addRow(new Object[]{v.get(i).getBlogId(), v.get(i).getTitle(), v.get(i).getContent(),
                        v.get(i).getPostTime(), v.get(i).getUserId()});
            }
        } catch (SQLException sqle) {
            System.out.println(sqle);
        } catch (Exception e) {
            System.out.println(e.getMessage());
        }
        return tableModel;
    }
    public static void main(String[] args) {
        blogtable w = new blogtable();
        w.setVisible(true);
    }
}

